<?php
namespace Home\Controller;
use Think\Controller;
header('content-type:text/html;charset=utf-8');

class CollectController extends Controller{
	public function index(){
		$collect=M('collect');
		$user=$_SESSION['username'];

		$collection=$collect->where("user_name="."'".$user."'")
		                    ->order('collect_id desc')->select();

		$this->assign('collect',$collection);
		$this -> display();
	}
	//添加收藏
	public function add(){

		//实例化商品表
		if($_SESSION['username']==null){
			$this->success("请先登录,再收藏");
			die;
		}else{
		$good=M('goods');
		$goods=$good->where('goodsid='.$_GET['goodsid'])->find();
		//将值付给收藏表
		$data['price']=$goods['price'];
		$data['goodsname']=$goods['goodsname'];
		$data['img']=$goods['goodspic'];
		$data['user_name']=$_SESSION["username"];
		$data['goodsid']=$_GET["goodsid"];
		//实例化收藏表

		$collect=M('collect');
		$user=$_SESSION['username'];
		//多条件查询用数组
		$arr['goodsid']=$_GET['goodsid'];
		$arr['user_name']=$_SESSION['username'];
		//判断是否已经收藏

		$collection=$collect->where($arr)->find();

		if($collection){
			$this->success("已收藏过");
		}else{
		if($collect->add($data)){
			$this->success('收藏成功');
		}else{
			$this->error('收藏失败');
		}
	}
}
}
        //删除收藏
      public function del(){
      	$collect=M('collect');

      	$collection=$collect->where('collect_id='.$_GET['id'])->delete();

      	if($collection){
      		$this->success('删除成功');
      	}else{
      		$this->error('删除失败');
      	}
      }
	
}
